package com.googlecode.connectlet.crypto.cipher.whitening;

import com.googlecode.connectlet.crypto.cipher.Encipheror;
import com.googlecode.connectlet.util.Bytes;

public class WhiteningEnc implements Encipheror {
	private static final long serialVersionUID = 1L;

	private Encipheror cipher;
	private int blockSize;
	private byte[] inKey, outKey;

	public WhiteningEnc(Encipheror cipher, byte[] key) {
		this.cipher = cipher;
		blockSize = cipher.getBlockSize();
		this.inKey = Bytes.left(key, blockSize);
		this.outKey = Bytes.right(key, blockSize);
	}

	@Override
	public int getBlockSize() {
		return blockSize;
	}

	@Override
	public void encrypt(byte[] src, int srcPos, byte[] dest, int destPos) {
		Bytes.xor(src, 0, inKey, 0, dest, 0, blockSize);
		cipher.encrypt(dest, destPos, dest, destPos);
		Bytes.xor(dest, 0, outKey, 0, dest, 0, blockSize);
	}
}